import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
import store from '@/store'

var constantRouterMap = [];

function registerComponents() {
  // We repeat this code in other register functions, because if we don't use a literal for the path
  // Webpack won't be able to statcily analyze our imports.
  const context = require.context('@/views', true, /^((?![\\/.]test[\\./]).)*\.vue?$/);
  registerAll(context);
}

function registerAll(context) {
  const modules = context
    .keys()
    .map(context)
    .map(module => module.default).filter(f => f.page);
  console.log(modules)

  const arr = modules.map((item) => {
    let _item = {};
    _item.path = `/${item.name}`;
    _item.title = item.title;
    //判断当前模块需不需要公用模块

    if (item.block) {
      _item.component = () => import(`@/views/${item.block.page}/index`);
      _item.children = [];
      _item.children.push({
        path: '',
        component: () => import(`@/${item.__file.split('.vue')[0].split('src/')[1]}`)
      })
    } else {
      _item.component = () => import(`@/${item.__file.split('.vue')[0].split('src/')[1]}`);
    }
    if (item.navView) {
      store.dispatch('addNavsView', _item)
    }
    return _item
  })
  constantRouterMap = arr;
  // constantRouterMap = [{
  //     path: '/404',
  //     component: () => import('@/views/errorPage/404'),
  //   },
  //   {
  //     path: '/401',
  //     component: () => import('@/views/errorPage/401'),
  //   },
  //   {
  //     path: '/login',
  //     component: () => import('@/views/login/index'),
  //   },
  //   {
  //     path: '/dashboard',
  //     component: () => import('@/views/layout/index'),
  //     children: [{
  //       path: '',
  //       component: () => import('@/views/dashboard/index'),
  //     },
  //     //可以扩展
  //     // {
  //     //   path: '1',
  //     //   component: () => import('@/views/login/index'),
  //     // }
  //   ]
  //   },
  //   {
  //     path: '',
  //     name: 'home',
  //     component: Home
  //   },
  //   {
  //     path: '/about',
  //     name: 'about',
  //     // route level code-splitting
  //     // this generates a separate chunk (about.[hash].js) for this route
  //     // which is lazy-loaded when the route is visited.
  //     component: () => import( /* webpackChunkName: "about" */ './views/About.vue')
  //   }
  // ];
  // return modules
  //   .filter(isFunction)
  //   .filter(f => f.init)
  //   .map(f => f(ngModule));
}

registerComponents()

console.log(constantRouterMap);
//路由条件不满足跳转404
constantRouterMap.push({ path: '/', redirect: '/dashboard'});
constantRouterMap.push({ path: '*', redirect: '/404'});
export default new Router({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes: constantRouterMap
})